package com.xy.frame.listener;

import com.xy.frame.WEBApp;
import com.xy.frame.service.SysconfigService;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;


/**
 * Created by admin on 2014/3/27.
 */
public class XYApplicationContextListener implements ServletContextListener {
	private Logger logger = LoggerFactory.getLogger(XYApplicationContextListener.class);
	static {
		System.setProperty("java.awt.headless", "true");
	}

	public void contextInitialized(ServletContextEvent servletContextEvent) {
		WEBApp.SPRING_CONTEXT = WebApplicationContextUtils.getWebApplicationContext(servletContextEvent
				.getServletContext());
		logger.info(".........初始化系统参数开始..................");
		try {
			// 加载模板配置
			SysconfigService sysconfig =	WEBApp.SPRING_CONTEXT.getBean(SysconfigService.class);
			WEBApp.addCODE(sysconfig.loadCodeValue());
			
			// 加载WS 请求URL
			servletContextEvent.getServletContext().setAttribute("staticUrl", servletContextEvent.getServletContext().getContextPath());
			//当前时间
			servletContextEvent.getServletContext().setAttribute("staticV", String.valueOf(System.currentTimeMillis()));

            logger.info("..........初始化系统参数结束..................");
		} catch (Exception e) {
			logger.error("初始化系统参数失败！" + e.getMessage(), e);
			//throw new RuntimeException("初始化系统参数失败！" + e.getMessage(), e);
		}
	}

	public void contextDestroyed(ServletContextEvent servletContextEvent) {
	}
}
